# coding=utf-8
#

import json
import logging
import threading
from django.conf import settings
from logging.handlers import RotatingFileHandler

_LOCALS = threading.local()

LOG_LEVEL = logging.INFO


if settings.DEBUG:
    LOG_FILE = "logs/sm_log.log"
else:
    LOG_FILE = settings.LOG_FILE


def getLogger(package_name):
    logger = getattr(_LOCALS, package_name, None)
    if logger is not None:
        return logger

    logger = logging.getLogger(package_name)
    hdlr = RotatingFileHandler(LOG_FILE, maxBytes=1024*1024*50, backupCount=10, encoding="utf-8")
    formatter = logging.Formatter('[%(asctime)s] Level:%(levelname)s location:%(name)s FuncName:%(funcName)s Line:%(lineno)d Message:%(message)s','%Y-%m-%d %a %H:%M:%S')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(LOG_LEVEL)
    setattr(_LOCALS, package_name, logger)
    return logger
